#!/bin/bash

if [ $# -gt 0 ]; then
	echo "Changing to commit $1" |& tee --append install-core-v-mcu.log
	(cd core-v-mcu; git checkout $1) |& tee --append install-core-v-mcu.log
fi
echo "######################################"
echo "#"
echo -n "# core-v-mcu branch:"
(git branch --list)
echo "#"
echo "######################################"

(
     echo "######################################" \
  && echo "#" \
  && echo "# run ioscript.py" \
  && echo "#" \
  && echo "######################################" \
  && python3 util/ioscript.py\
      --soc-defines rtl/includes/pulp_soc_defines.svh\
      --peripheral-defines rtl/includes/pulp_peripheral_defines.svh\
      --periph-bus-defines rtl/includes/periph_bus_defines.sv\
      --pin-table pin-table.csv\
      --perdef-json perdef.json\
      --pad-control rtl/core-v-mcu/top/pad_control.sv\
      --pad-frame-sv rtl/core-v-mcu/top/pad_frame.sv\
      --xilinx-core-v-mcu-sv fpga/core-v-mcu-nexys/rtl/xilinx_core_v_mcu.v\
      --input-xdc fpga/core-v-mcu-nexys/constraints/Nexys-A7-100T-Master.xdc\
      --output-xdc fpga/core-v-mcu-nexys/constraints/core-v-mcu-pin-assignment.xdc\
      --cvmcu-h core-v-mcu-config.h\
  && echo "############# Done ###################" \
) |& tee --append install-core-v-mcu.log

grep '### Done ###' install-core-v-mcu.log
if [ $? -ne 0 ]; then
  exit 1
fi
